<style>
    .w-120{
        width: 115px;
    }
    .conentStyle{
        padding: 10px;
        box-sizing: border-box;
    }
    .conentBg{
        padding: 20px;
        box-sizing: border-box;
        border-radius: 5px;
        background-color: #fff;
        min-height: 660px;
    }
    .borderNone{
        border: none;
    }
    .formBg{
        background-color: #F7F8FA;
        padding: 20px;
    }
    .layui-table-cell{
        height: auto;
    }
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">计划单编号：</label>
                                    <div class="layui-input-inline">
                                        <input name="code" class="layui-input" type="text" placeholder="计划单编号"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">出库类型：</label>
                                    <div class="layui-input-inline mr0">
                                        <select name="inoutTypeCode" lay-filter="inoutTypeCode">
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">按发货仓：</label>
                                    <div class="layui-input-inline">
                                        <select name="outWarehouseId" lay-filter="outWarehouseId" id="outWarehouseNameSelect">
                                            <option value="">按发货仓</option>
                                        </select>
                                    </div>
                                </div>
                                <!--<div class="layui-inline">
                                    <label class="layui-form-label w-auto">按收货仓：</label>
                                    <div class="layui-input-inline">
                                        <select name="inWarehouseId" lay-filter="inWarehouseId" id="inWarehouseNameSelect">
                                            <option value="">按收货仓</option>
                                        </select>
                                    </div>
                                </div>-->
                                <!--<div class="layui-inline">
                                    <label class="layui-form-label w-auto">按发货人：</label>
                                    <div class="layui-input-inline">
                                        <select name="typeOfEnterprise">
                                            <option value="">发货人</option>
                                            <option value="0">马云飞</option>
                                            <option value="1">刘总</option>
                                            <option value="2">莫丹</option>
                                            <option value="3">政哥</option>
                                        </select>
                                    </div>
                                </div>-->

                               <!-- <div class="layui-inline">
                                    <label class="layui-form-label w-auto">商品名称：</label>
                                    <div class="layui-input-inline">
                                        <input name="productName" class="layui-input" type="text" placeholder="商品名称"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">商品编码：</label>
                                    <div class="layui-input-inline">
                                        <input name="skuCode" class="layui-input" type="text" placeholder="商品编码"/>
                                    </div>
                                </div>-->
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">创建时间：</label>
                                    <div class="layui-input-inline">
                                        <input  id="pTime" class="layui-input date-icon" type="text" placeholder="选择创建时间" autocomplete="off" readonly/>
                                    </div>
                                    <input name="startCreateTime" id="sTime" style="display:none"/>
                                    <input name="endCreateTime" id="eTime" style="display:none"/>
                                </div>
                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" id="formSubSearchLog" lay-filter="formSubSearchLog"  lay-submit>查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button type="button" id="inventoryOutPlanBtnAdd" class="layui-btn icon-btn color-reseda">添加调拨出库计划</button>
                        </div>
                        <div class="lay-btn-rt">
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="inventoryOutPlanTable" lay-filter="inventoryOutPlanTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="inventoryOutPlanTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="details">详情</a>
    {{# if(d.status =="confirmed"){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="audit">审核</a>
    {{# } }}
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="inventoryOutPlanForm">
    <form lay-filter="inventoryOutPlanForm" id="inventoryOutPlanForm2" class="layui-form open-form">
        <div class="open-model">
            <div class="open-auto">
                <input name="id" type="hidden"/>
                <div id="planCodeSpan" class="layui-form-item" style="display: none">
                    <label class="layui-form-label" style="width: 100px;"><span style="color: red">*</span>出库计划编号</label>
                    <div class="layui-form-mid layui-word-aux">
                        <span style="color: #ff0000" id="planCode">P20220506001</span>
                    </div>
                </div>
                <div id="statusDescSpan" class="layui-form-item" style="display: none">
                    <label class="layui-form-label" style="width: 100px;">状态</label>
                    <div class="layui-form-mid layui-word-aux">
                        <span style="color: red" id="statusDesc">待确认</span>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label" style="width: 100px;"><span style="color: red">*</span>发货商</label>
                    <div class="layui-form-mid layui-word-aux">
                        <span style="color: red" id="enterpriseName"></span>
                    </div>
                </div>
                <div id="inoutTypeIdSelect" class="layui-form-item" style="display: none">
                    <label class="layui-form-label" style="width: 100px;padding-left: 16px;"><span
                            style="color: red">*</span>出库类型</label>
                    <div class="layui-input-block" style="width: 200px;margin-left: 132px;">
                        <select name="inoutTypeCode" required lay-verify="required"  lay-filter="inoutTypeId" placeholder="请选择出库类型">
                            <!-- <option value="">出库类型</option>
                             <option value="1">调拨到云仓入库</option>-->
                            <!-- <option value="2">销售出库</option>
                             <option value="3">调拨出库</option>-->
                        </select>
                    </div>
                </div>
                <div id="outWarehouseIdSelect" class="layui-form-item" style="display: none">
                    <label class="layui-form-label" style="width: 100px;padding-left: 16px;"><span
                            style="color: red">*</span>出货仓库</label>
                    <div class="layui-input-block" style="width: 200px;margin-left: 132px;">
                        <select name="outWarehouseId" id="outWarehouseOption" required lay-verify="required" placeholder="请选择出货仓库">

                        </select>
                    </div>
                </div>
                <div id="outWarehouseIdSpan" class="layui-form-item" style="display: none">
                    <label class="layui-form-label" style="width: 100px;"><span style="color: red">*</span>出货仓库</label>
                    <div class="layui-form-mid layui-word-aux">
                        <span style="color: red" id="outWarehouseId"></span>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label" style="width: 100px;">发货人</label>
                    <div class="layui-form-mid layui-word-aux">
                        <span style="color: red" id="userName">刘智</span>
                    </div>
                </div>
                <div id="sendTimeInput" class="layui-form-item" style="display: none">
                    <label class="layui-form-label" style="width: 100px">发货时间</label>
                    <div class="layui-input-block" style="width: 200px;margin-left: 132px;">
                        <input type="text" name="sendTime" id="sendTime"  placeholder="请选择发货时间" autocomplete="off"  class="layui-input">
                    </div>
                </div>
                <div id="sendTime2Span" class="layui-form-item" style="display: none">
                    <label class="layui-form-label" style="width: 100px;">发货时间</label>
                    <div class="layui-form-mid layui-word-aux">
                        <span style="color: red" id="sendTime2"></span>
                    </div>
                </div>
                <div id="inWarehouseIdSelect" class="layui-form-item" style="display: none">
                    <label class="layui-form-label" style="width: 100px;padding-left: 16px;"><span
                            style="color: red">*</span>选择目标仓</label>
                    <div class="layui-inline">
                        <select name="province" lay-filter="province"></select>
                    </div>
                    <div class="layui-inline">
                        <select name="city" lay-filter="city"></select>
                    </div>
                    <div class="layui-inline">
                        <select name="inWarehouseId" id="inWarehouseOption" lay-filter="inWarehouseId" placeholder="请选择收货仓库">
                        </select>
                    </div>
                </div>

                <div id="cabinetSelect" class="layui-form-item" style="display: none">
                    <label class="layui-form-label" style="width: 100px;padding-left: 16px;"><span
                            style="color: red">*</span>选择目标柜子</label>
                    <div class="layui-input-block" style="width: 200px;margin-left: 132px;">
                        <select name="cabId"  lay-filter="cabId"  placeholder="请选选目标柜子">
                        </select>
                    </div>
                </div>

                <div id="inWarehouseIdSpan" class="layui-form-item" style="display: none">
                    <label class="layui-form-label" style="width: 100px;"><span style="color: red">*</span>目标仓库</label>
                    <div class="layui-form-mid layui-word-aux">
                        <span style="color: red" id="provinceName"></span>
                    </div>
                    <div class="layui-form-mid layui-word-aux">
                        <span style="color: red" id="cityName"></span>
                    </div>
                    <div class="layui-form-mid layui-word-aux">
                        <span style="color: red" id="inWarehouseId"></span>
                    </div>
                </div>
                <div id="cabinetNameSpan" class="layui-form-item" style="display: none">
                    <label class="layui-form-label" style="width: 100px;"><span style="color: red">*</span>目标柜子</label>
                    <div class="layui-form-mid layui-word-aux">
                        <span style="color: red" id="cabinetName"></span>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label" style="width: 100px;">发货商品</label>
                </div>
                <table class="layui-table" id="deliverProductTable" lay-filter="deliverProductTable"></table>
                <div class="layui-form-item" id="deliverProductBtn" style="display: none">
                    <label class="layui-form-label" style="width: 100px;"></label>
                    <div class="layui-form-mid layui-word-aux">
                        <button id="deliveryProductBtnAdd" type="button" class="layui-btn icon-btn"><i
                                class="layui-icon">&#xe654;</i>添加出库商品
                        </button>
                    </div>
                </div>
                <div id="remarkInput" class="layui-form-item" style="display: none">
                    <label class="layui-form-label" style="width: 100px">商品描述</label>
                    <div class="layui-input-block" style="width: 200px;margin-left: 132px;">
                        <input type="text" name="remark" placeholder="请输入商品描述" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
                <div id="remarkDescSpan" class="layui-form-item" style="display: none">
                    <label class="layui-form-label" style="width: 100px;">商品描述</label>
                    <div class="layui-form-mid layui-word-aux">
                        <span style="color: red" id="remarkDesc"></span>
                    </div>
                </div>
            </div>
            <div class="layui-form-item text-right">
                <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
                <button class="layui-btn" lay-filter="inventoryOutPlanFormSubmit" lay-submit>提交</button>
            </div>
        </div>
    </form>
</script>

<script type="text/html" id="inventoryOutProductForm">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <form class="layui-form" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <select name="productTypeId" lay-filter="productTypeId" id="productTypeId">
                                <option value="">类型</option>
                            </select>
                        </div>
                        <div class="layui-inline">
                            <select name="productCategoryId" lay-filter="productCategoryId" id="productCategoryId">
                                <option value="">物料分类</option>
                            </select>
                        </div>

                        <!--<div class="layui-inline">
                            <select name="status" lay-filter="status">
                                <option value="">按状态</option>
                                <option value="stockup">备货中</option>
                                <option value="reviewed">待复核</option>
                                <option value="warehouse">待出库</option>
                                <option value="sign">待签收</option>
                                <option value="complete">完成</option>
                            </select>
                        </div>-->
                        <div class="layui-inline">
                            <input name="skuSpecValueNames" class="layui-input" type="text" placeholder="规格码"/>
                        </div>
                        <div class="layui-inline">
                            <input name="productName" class="layui-input" type="text" placeholder="物料名称"/>
                        </div>
                        <div class="layui-inline">
                            <input name="skuCode" class="layui-input" type="text" placeholder="物料编号"/>
                        </div>
                        <div class="layui-inline">
                            <button  class="layui-btn icon-btn" lay-submit=""
                                     lay-filter="productSearch"><i class="layui-icon">&#xe615;</i>搜索
                            </button>
                        </div>
                    </div>
                </form>
                <table class="layui-table" id="productTable" lay-filter="productTable"></table>
                <div class="layui-form-item" style="margin-top: 20px;text-align: center;">
                    <div class="layui-inline">
                        <button id="productConfirmBtn" type="button" lay-event="getCheckData" class="layui-btn icon-btn">确定提交 </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</script>
<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin','laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var laydate = layui.laydate;
        var type = 0; //0是添加 1是详情
        var productSkuCodeArr = [];
        var deliverProductList = [];//出库物料数据
        var outData = null;
        var inWarehouseName = "";
        var outBankType = "";
        //日期范围
        laydate.render({
            elem: '#pTime',
            type: 'date',
            range: true,
            done: function(value, date, endDate){
               if(value){
                   let dateValue = value.split(" - ");
                   $("input[name='startCreateTime']").val(dateValue[0] + " 00:00:00");
                   $("input[name='endCreateTime']").val(dateValue[1] + " 23:59:59");
               }else {
                   $("input[name='startCreateTime']").val("");
                   $("input[name='endCreateTime']").val("");
               }
            }
        });
        // option渲染
        function loadSelectHtml(data, tips, htmltarget, type) {
            let html = ' <option value>' + tips + '</option>';
            if (!data || data == null) {
                return;
            }
            for (let i = 0; i < data.length; i++) {
                if (type == 1) {
                    html += `<option value='${data[i].productType}'>${data[i].name}</option>`;
                } else if (type == 2) {
                    html += `<option value='${data[i].id}'>${data[i].name}</option>`;
                } else if (type == 3) {
                    html += `<option value='${data[i].id}'>${data[i].warehouseName}</option>`;
                }else if(type == 4){
                    html += `<option value='${data[i].skuCode}'  skuCode='${data[i].skuCode}'>${data[i].skuName}</option>`;
                } else if(type == 5){
                    html += `<option value='${data[i].skuCode}'>${data[i].skuName}</option>`;
                } else if(type == 6){
                    html += `<option value='${data[i].code}'>${data[i].name}</option>`;
                }

            }

            $("form select[name='" + htmltarget + "']").html(html);
            form.render('select');
        }


        //查询仓库
        admin.req('enterprisecab/warehouse/admin/warehouse/getWarehouseVOAll', {}, function (res) {
            if (res.code == 200) {
                if (res.data.length > 0){
                    $.each(res.data,function (index,item) {
                        $("#outWarehouseNameSelect").append(new Option(item.name,item.id));
                    })
                    $.each(res.data,function (index,item) {
                        $("#inWarehouseNameSelect").append(new Option(item.name,item.id));
                    })
                    form.render('select');
                }
            } else {
                layer.msg(res.msg, {icon: 2});
            }
        }, 'GET');
        //查询出库类型
        admin.req('enterprisecab/warehouse/admin/warehouseInventoryType/list', {inventoryType: 'out',limit:9999,status:'enable'}, function (res) {
            if (res.code == 200) {
                if (res.data.length > 0){
                    loadSelectHtml(res.data, "选择出库类型", "inoutTypeCode", 6);
                }
            } else {
                layer.msg(res.msg, {icon: 2});
            }
        }, 'GET');
        // 渲染表格
        var insTb = table.render({
            elem: '#inventoryOutPlanTable',
            url: config.base + 'enterprisecab/order/admin/inventoryOutPlan/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'}
                ,{field: 'code', sort: true, title: '出库计划单编号', width: 150}
                ,{field: 'outWarehouseName', title: '发货仓库'}
                ,{field: 'consigner', title: '发货人'}
                ,{field: 'quantityTotal', title: '出库商品数量', width: 150}
                ,{field: 'inoutTypeName', title: '出库类型', width: 150}
                // ,{field: 'supplyAmount', sort: true, title: '出库商品单价', width: 150}
                ,{field: 'marketAmount', title: '市场总价', width: 160}
                ,{field: 'supplyAmount', title: '总供货金额'}
                ,{field: 'inWarehouseName',title: '入库云仓'}
                //,{field: 'no', sort: true, title: '联系人'}
                ,{field: 'inAreaName',title: '仓库所在地'}
                ,{field: 'statusDesc',title: '状态'}
                ,{field: 'createTime', sort: true, title: '添加时间', width: 160}
                ,{align: 'center', toolbar: '#inventoryOutPlanTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {
                //移除按钮
              /*  var menuButton = config.getMenuButton(location.hash);
                if(menuButton.indexOf("搜索") == -1){
                    $('.layui-form.toolbar').remove();
                }
                if(menuButton.indexOf("修改") == -1){
                    $("[lay-event='edit']").remove();
                }
                if(menuButton.indexOf("删除") == -1){
                    $("[lay-event='del']").remove();
                }*/
            }
        });
        form.render('select');
        // 工具条点击事件
        table.on('tool(inventoryOutPlanTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'details') { //详情
                type = 1;
                outData = data;
                showEditModel(type,outData);
            } else if (obj.event === 'audit') { // 审核
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(inventoryOutPlanTable)', function(obj){
            table.reload('inventoryOutPlanTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
            return false;
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            let field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
        });

        // 添加按钮点击事件
        $('#inventoryOutPlanBtnAdd').click(function () {
            type = 0;
            outData = null;
            showEditModel(type,outData);
        });

        // 显示编辑弹窗
        function showEditModel(type,data) {
            admin.open({
                type: 1,
                area: ['1400px', '90%'],
                offset: '65px',
                title: data ? '修改出库单计划' : '添加出库单计划',
                content: $('#inventoryOutPlanForm').html(),
                success: function () {
                    //查询企业
                    admin.req('enterprisecab/common/admin/cabCommon/getEnterpriseName', {}, function (res) {
                        if (res.code == 200) {
                            $("#enterpriseName").html(res.msg);
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GET');
                    if(type == 0){//添加
                        $("#inoutTypeIdSelect").show();
                        $("#outWarehouseIdSelect").show();
                        $("#sendTimeInput").show();
                        /*$("#inWarehouseIdSelect").show();
                        $("#cabinetSelect").show();*/
                        $("#deliverProductBtn").show();
                        $("#remarkInput").show();
                        let userName = config.getUser().nickname;
                        $("#userName").html(userName);
                        //查询仓库
                        admin.req('enterprisecab/warehouse/admin/warehouse/getWarehouseVOAll', {}, function (res) {
                            if (res.code == 200) {
                                if (res.data.length > 0){
                                    loadSelectHtml(res.data, "选择出货仓库", "outWarehouseId", 2);
                                    //loadSelectHtml(res.data, "选择目标仓", "inWarehouseId", 2);
                                }
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET');
                        //查询出库类型
                        admin.req('enterprisecab/warehouse/admin/warehouseInventoryType/list', {inventoryType: 'out',limit:9999,status:'enable'}, function (res) {
                            if (res.code == 200) {
                                if (res.data.length > 0){
                                    loadSelectHtml(res.data, "选择出库类型", "inoutTypeCode", 6);
                                }
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET');

                        // 请求省份数据
                        function getProvinceData(){
                            admin.req('enterprisecab/area/admin/area/getProvince', {}, function (res) {
                                if (200 == res.code) {
                                    loadSelectHtml(res.data, "请选择省份", "province", 2);
                                }
                            }, 'GET', true);
                        }

                        // 省份下拉事件
                        form.on('select(province)', function(data){
                            getCityData(data.value)
                        });

                        // 请求城市数据
                        function getCityData(_parentId){
                            if(_parentId){
                                admin.reqSync('enterprisecab/area/admin/area/getCity', {parentId:_parentId}, function (res) {
                                    if (200 == res.code) {
                                        loadSelectHtml(res.data, "请选择城市", "city", 2);
                                    }
                                }, 'GET', true);
                            }else {
                                loadSelectHtml([], "请选择城市", "city", 2);
                            }

                        }
                        // 城市下拉操作
                        form.on('select(city)', function(data){
                            getPlatformWarehouseData($('form[lay-filter="inventoryOutPlanForm"] select[name="province"]').val(),data.value)
                        });

                        //请求目标仓库
                        function getPlatformWarehouseData(_provinceId,_cityId){
                            if(_provinceId && _cityId){
                                admin.req('enterprisecab/warehouse/admin/warehouse/getPlatformWarehouseAll', {province:_provinceId,city:_cityId}, function (waRes) {
                                    if (waRes.code == 200) {
                                        if (waRes.data.length > 0){
                                            loadSelectHtml(waRes.data, "选择目标仓", "inWarehouseId", 2);
                                            /* $.each(waRes.data,function (index,item) {
                                                 $("#inWarehouseOption").append(new Option(item.name,item.id));
                                             })
                                             form.render('select');*/
                                        }
                                    } else {
                                        layer.msg(waRes.msg, {icon: 2});
                                    }
                                }, 'GET');
                            }else {
                                loadSelectHtml([], "选择目标仓", "inWarehouseId", 2);
                            }
                        }

                        //监听出库类型
                        form.on('select(inoutTypeId)', function(data){
                            getCityData();
                            getPlatformWarehouseData();
                            //$("#deliveryProductBtnAdd").show();
                            //查询仓库
                            //出库类型是调拨到云仓入库
                            outBankType = data.value;
                            if(data.value.indexOf("allotCloudWarehouse") == 0){
                                $("#cabinetSelect").hide();
                                $("#inWarehouseIdSelect").show();

                                $("select[name='cabId']").attr('lay-verify', 'none');
                                $("select[name='inWarehouseId']").attr('lay-verify', 'required');

                                $("#inWarehouseOption").html("");//每次先清空
                                admin.req('enterprisecab/order/admin/inventoryOutPlan/getCloudProvider', {}, function (res) {
                                    if (res.code == 200) {
                                        if(res.data == false){
                                            layer.msg("目前还不是云仓供应商，需前往设置！", {icon: 2,time:2000, shade:0.4});
                                            setTimeout(function (){
                                                console.log("跳转页面")
                                            },2000);
                                        }
                                    } else {
                                        layer.msg(res.msg, {icon: 2});
                                    }
                                }, 'GET');


                            //出库类型是调拨到云仓的话需要查询平台端的仓库数据 其他类型则是查询企业自有的仓库
                            getProvinceData();
                            }else if(data.value.indexOf("shippingPlan") == 0){
                                //查询柜子信息
                                $("#inWarehouseIdSelect").hide();
                                $("#cabinetSelect").show();
                                $("select[name='cabId']").attr('lay-verify', 'required');
                                $("select[name='inWarehouseId']").attr('lay-verify', 'none');
                                admin.req('enterprisecab/cabinet/admin/cabinet/getCabinetAll', {}, function (cabRes) {
                                    if (cabRes.code == 200) {
                                        if (cabRes.data.length > 0){
                                            loadSelectHtml(cabRes.data, "选择目标柜子", "cabId", 2);
                                            /* $.each(waRes.data,function (index,item) {
                                                 $("#inWarehouseOption").append(new Option(item.name,item.id));
                                             })
                                             form.render('select');*/
                                        }
                                    } else {
                                        layer.msg(res.msg, {icon: 2});
                                    }
                                }, 'GET');
                            }else{
                                getCityData();
                                getPlatformWarehouseData();
                                $("#inWarehouseIdSelect").hide({duration:300});
                                $("#cabinetSelect").hide({duration:300});
                                //查询仓库
                                admin.req('enterprisecab/warehouse/admin/warehouse/getWarehouseVOAll', {}, function (res) {
                                    if (res.code == 200) {
                                        if (res.data.length > 0){
                                            loadSelectHtml(res.data, "选择目标仓", "inWarehouseId", 2);
                                        }
                                    } else {
                                        layer.msg(res.msg, {icon: 2});
                                    }
                                }, 'GET');
                            }
                        })
                        //监听目标仓库
                        form.on('select(inWarehouseId)', function(data){
                            inWarehouseName = data.elem[data.elem.selectedIndex].text;
                        })
                    }else{
                        $("#planCodeSpan").show();
                        $("#statusDescSpan").show();
                        $("#outWarehouseIdSpan").show();
                        $("#sendTime2Span").show();
                        $("#remarkDescSpan").show();
                        //详情需要查询
                        admin.req('enterprisecab/order/admin/inventoryOutPlan/getInventoryOutPlanById', {id:data.id}, function (res) {
                            if (res.code == 200) {
                                $("#planCode").html(res.data.code);
                                $("#statusDesc").html(res.data.statusDesc);
                                $("#userName").html(res.data.consigner);
                                $("#sendTime2").html(res.data.sendTime);
                                $("#remarkDesc").html(res.data.remark);
                                $("#outWarehouseId").html(res.data.outWarehouseName);

                                //出库类型
                                if(res.data.inoutTypeCode == "allotCloudWarehouse"){
                                    $("#inWarehouseIdSpan").show();
                                    $("#cabinetNameSpan").hide();
                                    $("#inWarehouseId").html(res.data.inWarehouseName + "(" + res.data.areaName + ")");
                                    $("#provinceName").html(res.data.provinceName);
                                    $("#cityName").html(res.data.cityName );
                                }else if(res.data.inoutTypeCode == "shippingPlan"){
                                    $("#inWarehouseIdSpan").hide();
                                    $("#cabinetNameSpan").show();
                                    $("#cabinetName").html(res.data.cabinetName);
                                }

                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET');
                        //出货商品详情
                        admin.req('enterprisecab/order/admin/inventoryOutPlan/getInventoryOutPlanDetailVOList', {planId:data.id}, function (res) {
                            if (res.code == 200) {
                                deliverProductList = res.data;
                                table.reload('deliverProductTable',{
                                    data: deliverProductList,
                                })
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET');


                    }

                    deliverProductList = [];//每次打开弹框清空出库物料数据
                    //发货时间
                    laydate.render({
                        elem: '#sendTime'
                        ,type: 'datetime'
                    });
                    //添加出库商品点击事件
                    $("#deliveryProductBtnAdd").click(function () {
                        showProductModel();
                    })
                    var deliverProductTable = table.render({
                        elem: '#deliverProductTable',
                        data: deliverProductList,
                        cellMinWidth: 100,
                        cols: [[
                            {field: 'number', title: '序号', width: 60, type: 'numbers'}
                            , {field: 'id', title: 'ID', width: 60, hide: true}
                            , {field: 'skuCode', sort: true, title: '平台物料编号', width: 150}
                            , {field: 'skuPic', sort: true, title: '图片', width: 100,hide : type == 0 ? true : false
                                ,templet:'<div><img  src="{{ d.skuPic }}"></div>'}
                            , {field: 'productPic', sort: true, title: '图片', width: 100,hide : type == 1 ? true : false
                                ,templet:'<div><img  src="{{ d.productPic }}"></div>'}
                            , {field: 'skuName', sort: true, title: '物料名称', width: 150}
                            , {field: 'skuSpecValueNames', title: '规格',hide : type == 1 ? true : false}
                            , {field: 'skuSpecCode', title: '规格',hide : type == 0 ? true : false}
                            , {field: 'enterpriceCode', title: '企业货号'}
                            , {field: 'barcode', title: '国标条码'}
                            , {field: 'unitName', title: '最小单位'}
                            , {field: 'supplyPrice', title: '平台结算价'}
                            , {field: 'quantity', title: '出库数量(可填写)',edit:true,hide : type == 1 ? true : false, width: 150}
                            , {field: 'quantity', title: '出库数量(可填写)',edit:true,hide : type == 0 ? true : false, width: 150}
                            //, {field: 'inventoryTotal', title: '最小单位库存',hide : type == 0 ? true : false, width: 120}
                            , {field: 'totalMoney', title: '总金额',hide : type == 0 ? true : false}
                        ]]
                        ,done: function(res, curr, count){
                            if(type == 1){
                                let tableView = this.elem.next();
                                layui.each(res.data, function (i, item) {
                                    if (data.status === 'confirmed') {
                                        tableView.find('tr[data-index=' + i + ']').find('td').eq(12).data('edit', true); //设置下标为9的单元格为可编辑
                                    }
                                })
                            }
                        }
                    });
                    //编辑出库商品计划出库数单元格事件
                    table.on('edit(deliverProductTable)', function(obj){ //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
                        /* console.log(obj.value); //得到修改后的值
                         console.log(obj.field); //当前编辑的字段名
                         console.log(obj.data); //所在行的所有相关数据*/
                        const row = deliverProductList.find(x => x.skuCode === obj.data.skuCode);
                        if(row){
                            row.quantity = obj.value;
                            table.reload('deliverProductTable',{
                                data: deliverProductList,
                            })
                        }
                        layer.msg("修改成功");
                    });
                    form.val('inventoryOutPlanForm', data);
                    // 表单提交事件
                    form.on('submit(inventoryOutPlanFormSubmit)', function (d) {
                        if(type == 0){//添加
                            if(deliverProductList.length == 0){
                                layer.msg("请先添加出库商品",{icon: 2});
                                return false;
                            }

                            d.field.productJson = JSON.stringify(deliverProductList);
                            d.field.inWarehouseName = inWarehouseName;
                            layer.load(2);
                            admin.req('enterprisecab/order/admin/inventoryOutPlan/add', d.field, function (res) {
                                layer.closeAll('loading');
                                if (res.code == 200) {
                                    layer.msg(res.msg, {icon: 1});
                                    table.reload('inventoryOutPlanTable');
                                    layer.closeAll('page');
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }, data ? 'PUT' : 'POST');
                        }else {
                            console.log("状态",data.status)
                            if(data.status != 'confirmed'){
                                layer.msg("此出库计划已经审核，不能再次提交",{icon: 2});
                                return false;
                            }
                            //详情修改出库数量
                            let deliverProductListNew = deliverProductList.map(item => {
                                return {id:item.id,quantity:item.quantity};
                            })
                            let productJson = JSON.stringify(deliverProductListNew);
                            admin.req('enterprisecab/order/admin/inventoryOutPlan/updateOutQuantity', {productJson:productJson}, function (res) {
                                layer.closeAll('loading');
                                if (res.code == 200) {
                                    layer.msg(res.msg, {icon: 1});
                                    table.reload('inventoryOutPlanTable');
                                    layer.closeAll('page');
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            },'PUT');
                        }
                        return false;
                    });
                }
            });
        }
        //enterprise/product/admin/productPackLabel/getProductPackLabelVOList
        function showProductModel() {
            var index = admin.open({
                type: 1,
                area: ['1400px', '800px'],
                offset: '65px',
                title: '添加出库计划商品信息',
                content: $('#inventoryOutProductForm').html(),
                success: function () {
                    form.render('select');
                    let url = "";
                    if( outBankType =="allotCloudWarehouse"){
                        url = "enterprise/product/admin/productDetail/getPlatformProductDetailVOList";
                    }else{
                        url = "enterprise/product/admin/productDetail/getProductDetailPageList";
                    }

                    var productTable = table.render({
                        elem: '#productTable',
                        url:config.base + url,
                        //url: config.base + outBankType =="allotCloudWarehouse" ? 'enterprise/product/admin/productDetail/getPlatformProductDetailVOList' : 'enterprise/product/admin/productDetail/getProductDetailPageList',
                        page: true,
                        cellMinWidth: 100,
                        cols: [[
                            {type: 'checkbox'}
                            ,{field: 'number', title: '序号', width: 60, type: 'numbers'}
                            , {field: 'id', title: 'ID', width: 60, hide: true}
                            , {field: 'productPic', title: '图片', width: 100,templet:'<div><img src="{{ d.productPic}}" style="height: 80px;width: 80px;"></div>'}
                            , {field: 'skuName',title: '物料名称', width: 120}
                            , {field: 'skuCode', title: '物料编号', width: 150}
                            , {field: 'skuSpecValueNames', title: '规格码'}
                            , {field: 'supplyPrice', title: '平台采购价'}
                            , {field: 'categoryName', title: '物料分类'}
                            , {field: 'brandName', title: '品牌'}
                            , {field: 'companyName', title: '制造企业'}
                            , {field: 'createTime', title: '接入时间'}
                        ]]
                    });
                    let typeUrl = "";
                    if( outBankType =="allotCloudWarehouse"){
                        typeUrl = "enterprise/product/admin/productType/platformProductTypeList";
                    }else{
                        typeUrl = "enterprise/product/admin/productType/getProductTypeVOAll";
                    }
                    //查询物料类型
                    admin.req(typeUrl, {}, function (res) {
                        if (res.code == 200) {
                            if (res.data.length > 0){
                                $.each(res.data,function (index,item) {
                                    $("#productTypeId").append(new Option(item.name,item.id));
                                })
                                form.render('select');
                            }
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GET');
                    //查询物料分类
                    let categoryUrl = "";
                    if( outBankType =="allotCloudWarehouse"){
                        categoryUrl = "enterprise/product/admin/productCategory/platformProductCategoryList";
                    }else{
                        categoryUrl = "enterprise/product/admin/productCategory/getAll";
                    }
                    admin.req(categoryUrl, {}, function (res) {
                        if (res.code == 200) {
                            if (res.data.length > 0){
                                $.each(res.data,function (index,item) {
                                    $("#productCategoryId").append(new Option(item.name,item.id));
                                })
                                form.render('select');
                            }
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GET');

                    // 搜索
                    form.on('submit(productSearch)', function (data) {
                        productTable.reload({where: data.field,page: {curr: 1}},'data');
                        return false;
                    });
                    $("#productConfirmBtn").click(function () {
                        //头工具栏事件
                        var checkRows = table.checkStatus('productTable');
                        if (checkRows.data.length == 0) {
                            layer.msg('请选择要确认出货的商品', {icon: 2});
                            return;
                        }
                        let flag = false;
                        // 确认出货的商品
                        checkRows.data.forEach((item, index) => {
                            const row = deliverProductList.find(x => x.skuCode === item.skuCode);
                            checkRows.data[index].quantity = 1;
                            if(row){
                                flag = true;
                                layer.msg('选择发货的商品中有重复存在的，请检查', {icon: 2});
                            }
                        })

                        if(!flag) {
                            deliverProductList.push(...checkRows.data)
                            //确认提交物料信息，重载deliverProductList
                            table.reload('deliverProductTable',{
                                data: deliverProductList,
                            })
                            layer.close(index);
                        }

                        /*admin.req('enterprisecab/order/admin/inventoryOutPlan/getProductDetailList', {productSkuCodeArr: productSkuCodeArr}, function (res) {
                            if (res.code == 200) {
                                deliverProductList = res.data;
                                for (let i = 0; i < deliverProductList.length; i++) {
                                    deliverProductList[i].quantity = 1;
                                }
                                //确认提交物料信息，重载deliverProductList
                                table.reload('deliverProductTable',{
                                    data: deliverProductList,
                                })
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET');*/
                    })

                }
            });
        }
        // 删除
        function doDelete(obj) {
            layer.confirm('确定要审核吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprisecab/order/admin/inventoryOutPlan/updateStatus',{id:obj.data.id}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        table.reload('inventoryOutPlanTable',{
                        })
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'PUT');
            });
        }

        // 导出excel
        $('#inventoryOutPlanBtnExport').click(function () {
            var checkRows = table.checkStatus('inventoryOutPlanTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>